﻿using ServiceWeb.DBGateway;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsBase;
using Microsoft.EntityFrameworkCore;
using System.Reflection.Emit;
using UtilsORM;
using UtilsORM.MySqlData;
using ServiceWeb.DBBase;
using NPOI.OpenXmlFormats.Dml.Diagram;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Masuit.Tools;

namespace ServiceWeb.WebAdmin
{
    public class ServiceEchartMap
    {
      
        public List<iot_device_model_variable> getDeviceVariables(string dguid)
        {
            string sql = "select v.* from iot_device  d inner join iot_device_model on d_model_guid=m_guid\r\ninner join iot_device_model_variable_relation a on mv_model_guid=m_guid \r\nINNER JOIN iot_device_model_variable  v ON V_GUID=mv_v_guid\r\n  where d_guid='" + dguid + "'";
            return MySqlYihongIot.GetList<iot_device_model_variable>(sql);
        }

        public DataTable getDeviceValue(string dguid)
        {
            string sql = @" select  max(vl_value) vl_value,vl_name,left(vl_add_time,16) min from iot_device_value_log 
    where vl_d_guid = '" + dguid+@"' and vl_add_time> date_add(NOW(), interval - 10 minute)
    group by left(vl_add_time, 16),vl_name order by left(vl_add_time,16)   desc ";
            return MySqlYihongIot.GetDataTable(sql);
        }
        //public DataTable getEchartData(string dguid)
        //{
        //    //X轴，分钟
        //    DataTable table =  getDeviceValue(dguid);

        //    // 对 Name 字段进行去重
        //    List<EchartTitle> xLists = new List<EchartTitle>();

        //    DataTable distinctTable = table.DefaultView.ToTable(true, "vl_name");

        //    // 输出去重后的 DataTable
        //    foreach (DataRow row in distinctTable.Rows)
        //    {
        //        //Console.WriteLine($" vl_name: {row["vl_name"]}");

        //        EchartTitle xList = new EchartTitle();
        //        xList.name = row["vl_name"];
        //        xLists.Add(xList);
        //    }

        //    //X轴项
        //    List<EchartXValue> xValues = new List<EchartXValue>();

        //    DataTable distinctTablemin = table.DefaultView.ToTable(true, "min");

        //    // 输出去重后的 DataTable
        //    foreach (DataRow row in distinctTablemin.Rows)
        //    {
        //        //Console.WriteLine($" vl_name: {row["vl_name"]}");
        //        EchartXValue xValue = new EchartXValue();
        //        xValue.name = row["min"];
        //        xValues.Add(xValue);
        //    }
        //    //线值
        //    List<EchartData> echartDatas = new List<EchartData>();

        //   // DataTable distinctTabledata = table.DefaultView.ToTable(true, "vl_value");

        //    // 输出去重后的 DataTable
        //    foreach (DataRow row in table.Rows)
        //    {
        //        //Console.WriteLine($" vl_name: {row["vl_value"]}");
        //        EchartData echartData = new EchartData();
        //        echartData.name = row["vl_name"];
        //        echartData.value = row["vl_value"];
        //        echartDatas.Add(echartData);
        //    }
        //}
    }
    public class EchartTitle
    {
        public string name { get; set; }
    }

    public class EchartXValue
    {
        public string name { get; set; }
    }

    public class EchartData
    {
        public string name { get; set; }
        public double value { get; set; }
    }
}
